package com.sheep.jucdemo.part1.part1_1;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ThreadTest1 {
    public static class MyThread extends Thread{
        @Override
        public void run() {
            System.out.println("MyThread"+this.getName());
        }
    }

    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        myThread.start();
        new MyThread().start();

        MyRunnable myRunnable = new MyRunnable();
        new Thread(myRunnable).start();
        new Thread(myRunnable).start();

        MyRunnable1 myRunnable1a = new MyRunnable1("aa");
        new Thread(myRunnable1a).start();
        MyRunnable1 myRunnable1b = new MyRunnable1("bb");
        new Thread(myRunnable1b).start();

        FutureTask<String> stringFutureTask = new FutureTask<>(new MyCallable());
        Thread thread = new Thread(stringFutureTask);
        thread.start();
        try {
            String str = stringFutureTask.get();
            System.out.println(str);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }

    public static class MyRunnable implements Runnable{

        @Override
        public void run() {
            System.out.println("MyRunnable"+Thread.currentThread().getName());
        }
    }

    public static class MyRunnable1 implements Runnable{
        private String flag;

        public String getFlag() {
            return flag;
        }

        public void setFlag(String flag) {
            this.flag = flag;
        }

        public MyRunnable1() {
        }

        public MyRunnable1(String flag) {
            this.flag = flag;
        }

        @Override
        public void run() {
            System.out.println("MyRunnable"+Thread.currentThread().getName()+"flag"+flag);
        }
    }

    public static class MyCallable implements Callable<String> {

        @Override
        public String call() throws Exception {
            System.out.println("MyCallable "+Thread.currentThread().getName());
            return "hi";
        }
    }

}
